package com.powerunion.datacollection.util.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		
		HttpSession session = request.getSession(false);
		if(request.getHeader("x-requested-with")!=null      
	    && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
			System.out.println("x-requested-with！AuthFilter！");
			
		}
System.out.println("进来了！AuthFilter！");	    //response.addHeader("sessionstatus","timeout"); 


if (session == null) {
//			if(request.getHeader("x-requested-with")!=null      
//				    && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
				System.out.println("进来了！Session超时！");
				
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().print("nologin or timeout:" +
						"<script type='text/javascript'>" +
						
						"alert('超时！！')"+
						"</script>");
				   return;
				//}  

		}
				   filterChain.doFilter(request, response);
		
	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}
}
